package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsOnRtnTrade;
import com.hxqh.bo.dao.FpsOnRtnTradeDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsOnRtnTrade entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsOnRtnTrade
 * @author MyEclipse Persistence Tools
 */

public class FpsOnRtnTradeDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(FpsOnRtnTradeDAO.class);
	// property constants
	public static final String ORDER_REF = "orderRef";
	public static final String ORDER_LOCAL_ID = "orderLocalId";
	public static final String USER_ID = "userId";
	public static final String POLICY_ID = "policyId";
	public static final String FUTURES_CODE = "futuresCode";
	public static final String SELL = "sell";
	public static final String DIRECTION = "direction";
	public static final String TRADE_PRICE = "tradePrice";
	public static final String TRADE_NUM = "tradeNum";

	protected void initDao() {
		// do nothing
	}
	
	public void truncateTable() {
		getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql  = "truncate table fps_on_rtn_trade";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.executeUpdate();
				return null;
			}
			
		});
	}

	public void save(FpsOnRtnTrade transientInstance) {
		log.debug("saving FpsOnRtnTrade instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(FpsOnRtnTrade persistentInstance) {
		log.debug("deleting FpsOnRtnTrade instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsOnRtnTrade findById(java.lang.Long id) {
		log.debug("getting FpsOnRtnTrade instance with id: " + id);
		try {
			FpsOnRtnTrade instance = (FpsOnRtnTrade) getHibernateTemplate()
					.get("com.hxqh.bo.bean.FpsOnRtnTrade", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FpsOnRtnTrade instance) {
		log.debug("finding FpsOnRtnTrade instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsOnRtnTrade instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FpsOnRtnTrade as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByOrderRef(Object orderRef) {
		return findByProperty(ORDER_REF, orderRef);
	}

	public List findByOrderLocalId(Object orderLocalId) {
		return findByProperty(ORDER_LOCAL_ID, orderLocalId);
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByPolicyId(Object policyId) {
		return findByProperty(POLICY_ID, policyId);
	}

	public List findByFuturesCode(Object futuresCode) {
		return findByProperty(FUTURES_CODE, futuresCode);
	}

	public List findBySell(Object sell) {
		return findByProperty(SELL, sell);
	}

	public List findByDirection(Object direction) {
		return findByProperty(DIRECTION, direction);
	}

	public List findByTradePrice(Object tradePrice) {
		return findByProperty(TRADE_PRICE, tradePrice);
	}

	public List findByTradeNum(Object tradeNum) {
		return findByProperty(TRADE_NUM, tradeNum);
	}

	public List findAll() {
		log.debug("finding all FpsOnRtnTrade instances");
		try {
			String queryString = "from FpsOnRtnTrade";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FpsOnRtnTrade merge(FpsOnRtnTrade detachedInstance) {
		log.debug("merging FpsOnRtnTrade instance");
		try {
			FpsOnRtnTrade result = (FpsOnRtnTrade) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsOnRtnTrade instance) {
		log.debug("attaching dirty FpsOnRtnTrade instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsOnRtnTrade instance) {
		log.debug("attaching clean FpsOnRtnTrade instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsOnRtnTradeDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsOnRtnTradeDAO) ctx.getBean("FpsOnRtnTradeDAO");
	}
}